package com.example.wearing.user;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wearing.LoginActivity;
import com.example.wearing.MainActivity;
import com.example.wearing.R;
import com.example.wearing.find.PostDetailActivity;
import com.example.wearing.tools.ImageUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SettingActivity extends AppCompatActivity {

    private final static String TAG = "SettingActivity";
    String userNameStr, userImgStr, password1, password2;
    int userId;

    TextView userNameContent;
    ImageView userImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        init();
        //返回
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if(item.getItemId()==android.R.id.home){
            //返回"我的"界面
            Intent intent = getIntent();
            setResult(4, intent);
            Log.i(TAG, "onOptionsItemSelected: 返回啦");
            finish();
            return true;
        }
        super.onOptionsItemSelected(item);
        return true;
    }

    public void init(){
        //控件初始化
        userNameContent = findViewById(R.id.userNameContent);
        userImg = findViewById(R.id.userImg);

        //检查sharedPreference,获取用户名
        SharedPreferences sp = this.getSharedPreferences("user", Activity.MODE_PRIVATE);
        PreferenceManager.getDefaultSharedPreferences(this);
        userId = sp.getInt("user_id",0);
        userNameStr = sp.getString("user_name","");
        userImgStr = sp.getString("user_img","");
        userNameContent.setText(userNameStr);
        Bitmap bitmap = ImageUtil.base64ToImage(userImgStr);//设置头像
        userImg.setImageBitmap(bitmap);
    }

    //修改密码
    public void alterPassword(View view) {
        Intent intent = new Intent(this, AlterPasswordActivity.class);
        startActivity(intent);
    }

    //修改用户名
    public void alterUserName(View view) {
        Intent intent = new Intent(this, AlterUserNameActivity.class);
        startActivityForResult(intent, 3);
    }

    //退出登录
    public void logOutFun(View view) {
        //清空sharedpreference中内容
        SharedPreferences sp = getApplicationContext().getSharedPreferences("user", Activity.MODE_PRIVATE);
        sp.edit().clear().commit();

        //返回登陆界面
        Intent intent_login = new Intent();
        intent_login.setClass(this, LoginActivity.class);
        intent_login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent_login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent_login);
        finish();

    }

    public void alterUserImg(View view) {
        Intent intent = new Intent(this, AlterUserImgActivity.class);
        startActivityForResult(intent, 1);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 1 && resultCode == 2){
            //刷新界面
            //检查sharedPreference,获取用户头像
            SharedPreferences sp = this.getSharedPreferences("user", Activity.MODE_PRIVATE);
            PreferenceManager.getDefaultSharedPreferences(this);
            String userImgStr = sp.getString("user_img","");
            Bitmap bitmap = ImageUtil.base64ToImage(userImgStr);//设置头像
            userImg.setImageBitmap(bitmap);
        }else if(requestCode == 3 && resultCode == 4){
            //刷新界面
            //检查sharedPreference,获取用户名
            SharedPreferences sp = this.getSharedPreferences("user", Activity.MODE_PRIVATE);
            PreferenceManager.getDefaultSharedPreferences(this);
            userNameStr = sp.getString("user_name","");
            userNameContent.setText(userNameStr);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}